package com.xuerhua.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.List;

import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.model.FinalTermScoreReport;
import com.xuerhua.yxt.service.*;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.xuerhua.yxt.common.AppUser;
import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.ClassTeachers;
import com.xuerhua.yxt.model.StudentInfo;
import com.xuerhua.yxt.reqVo.BaseQueryVO;
import com.xuerhua.yxt.reqVo.ScoreSummaryQueryVO;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.utils.StringUtil;
import com.xuerhua.yxt.view.StudentInfoView;
import com.xuerhua.yxt.view.TeacherInfoView;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping(Const.TEACHER_ROLE + MenuConst.SCORE_REPORT)
@Controller
public class ScoreReportController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ScoreReportController.class);
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IScoreSummaryService scoreSummaryService;
    @Autowired
    private IClassTeachersService classTeachersService;
    @Autowired
    private IOrganizeGroupService organizeGroupService;
    //huhekun
    @Autowired
    private IFinalTermScoreReportService finalTermScoreReportService;

    @RequestMapping("student")
    public String studentList(Model model) {
        AppUser appUser = getAppUser();
        BaseQueryVO vo = new BaseQueryVO();
        vo.setFinishYear(getCurrentTerm().getFinishYear());
        vo.setId(appUser.getTeacher().getId());
        List<TeacherInfoView> teacherInfoView = teacherService.selectClassForTeacher(vo);
        if (!StringUtil.isNullOrEmpty(teacherInfoView)) {
            model.addAttribute("model", teacherInfoView.get(0));
        }
        int count = organizeGroupService.isLeaderGroup(appUser.getTeacher().getId());
        model.addAttribute("isleader", count);
        return "teacher/scoreReport/studentList";
    }

    @RequestMapping("clazz")
    public String clazzList() {
        return "teacher/scoreReport/clazzList";
    }

    @RequestMapping("studentScore/{studentId}")
    public String studentScore(@PathVariable Integer studentId, Model model) {
        StudentInfo student = studentService.selectById(studentId);
        if (student == null) return null;
        model.addAttribute("student", BeanUtils.copyAs(student, StudentInfoView.class));
        if (getCurrentTerm().getTermKey() - student.getGrade() > 3) {
            AppUser appUser = getAppUser();
            int count = organizeGroupService.isLeaderGroup(appUser.getTeacher().getId());
            if (count == 0) return null;
        }
        return "teacher/scoreReport/studentScore";
    }

    @RequestMapping("clazzScore/{grade}/{clazz}")
    public String clazzScore(@PathVariable Integer grade,
                             @PathVariable Integer clazz, Model model) {
        AppUser user = getAppUser();
        BaseQueryVO vo = new BaseQueryVO();
        vo.setSchoolId(user.getTeacher().getSchoolId());
        vo.setGrade(grade);
        vo.setClazz(clazz);
        vo.setFinishYear(getCurrentTerm().getFinishYear());
        List<ClassTeachers> teachers = classTeachersService.selectClassListAll(vo);
        if (StringUtil.isNullOrEmpty(teachers)) return null;
        model.addAttribute("teachers", teachers.get(0));
        return "teacher/scoreReport/clazzScore";
    }

    @RequestMapping("clazzScore/list")
    @ResponseBody
    Object clazzScoreList(ScoreSummaryQueryVO vo) {
        try {
            vo.setSchoolId(getAppUser().getTeacher().getSchoolId());
            return scoreSummaryService.selectScoreSummaryList(vo);
        } catch (Exception e) {
            logger.error("--clazzScoreList error={}", e);
        }
        return null;
    }

    @RequestMapping("clazzStudentScore/list")
    @ResponseBody
    Object clazzStudentScoreList(ScoreSummaryQueryVO vo) {
        try {
            int grade = vo.getGrade().intValue();
            int termKey = getCurrentTerm().getTermKey().intValue();
            int num = termKey - grade >= 5 ? 5 : (termKey - grade);
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i <= num; i++) {
                list.add(grade + i);
            }
            vo.setScoreList(list);
            vo.setSchoolId(getAppUser().getTeacher().getSchoolId());
            return scoreSummaryService.selectClazzStudentScoreList(vo);
        } catch (Exception e) {
            logger.error("---clazzStudentScoreList error={}", e);
        }
        return null;
    }

    /**
     * @author huhekun
     * @date 2019/12/26
     * @description 跳转到学生综合素质评价页面并进行相关查询操作
     */
    @RequestMapping("studentAssess/{studentId}")
    public String toModelStudentAccess(@PathVariable int studentId, Model model) {
        StudentInfo studentInfo = studentService.selectById(studentId);
        if (studentInfo == null) return null;
        model.addAttribute("student",BeanUtils.copyAs(studentInfo, StudentInfoView.class));
        return "teacher/scoreReport/studentReport";
    }

    /**
     * @author huhekun
     * @date 2019/12/27
     * @description 对指定id的学生的成绩和评价查询
     */
    @ResponseBody
    @RequestMapping(value = "studentGrade", method = RequestMethod.POST)
    public Object getStudentGrade(BaseQueryVO vo) {
        vo.setSchoolId(getAppUser().getTeacher().getSchoolId());
        FinalTermScoreReport finalTermScoreReport = finalTermScoreReportService.selectStudentTermReport(vo);
        return new RspResult(Status.SUCCESS, finalTermScoreReport);
    }
    /**
     * @author huhekun
     * @date 2020/1/6
     * @description 跳转到“期末成绩报告”的选学生页面
     */
    @RequestMapping("studentFinalTermReport")
    public String studentFinalTermReportList(Model model) {
        AppUser appUser = getAppUser();
        BaseQueryVO vo = new BaseQueryVO();
        vo.setFinishYear(getCurrentTerm().getFinishYear());
        vo.setId(appUser.getTeacher().getId());
        vo.setSchoolId(appUser.getTeacher().getSchoolId());
        List<TeacherInfoView> teacherInfoView = teacherService.selectClassForTeacher(vo);
        if (!StringUtil.isNullOrEmpty(teacherInfoView)) {
            model.addAttribute("model", teacherInfoView.get(0));
        }
        int count = organizeGroupService.isLeaderGroup(appUser.getTeacher().getId());
        model.addAttribute("isleader", count);
        return "teacher/scoreReport/studentFinalTermReportList";
    }
}
